//
//  ViewController.m
//  UI14_UICollectionView
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kCollectionView @"reuse"
#define kHead @"secReuse"
#define kFooter @"footerReuse"

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1. UICollectionView
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // UICollectionView 创建 填入不能为空的layout(布局)
    // item的大小 默认大小 (50, 50)
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 10, 100);
    // 滚动方向 (横向 纵向 默认纵向滚动)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 最小行间距 (行间距 列间距) (滚动方向改变 会改变行列)
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    // 边距 (上 左 下 右)
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 设置collectionVIew的  分区头大小 分区尾大小 (竖直方向可以为(0, 100) 水平方向可以设置为(100, 0))  (在使用中 头和尾都需要注册)
    layout.headerReferenceSize = CGSizeMake(50, 100);
    layout.footerReferenceSize = CGSizeMake(50, 200);
    // 悬浮效果
    layout.sectionHeadersPinToVisibleBounds = NO; // 让分区头悬浮
    
    
    
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:myCollectionView];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionView];
    
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHead];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter];
    
}
// 设置分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 37;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHead forIndexPath:indexPath];
        if (indexPath.section == 0) {
            reuseView.backgroundColor = [UIColor cyanColor];
        } else if (indexPath.section == 1) {
            reuseView.backgroundColor = [UIColor grayColor];
        } else {
            reuseView.backgroundColor = [UIColor blueColor];
        }
            return reuseView;
    } else {
        UICollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter forIndexPath:indexPath];
        if (indexPath.section == 0) {
            reuseView.backgroundColor = [UIColor greenColor];
        } else if (indexPath.section == 1) {
            reuseView.backgroundColor = [UIColor purpleColor];
        } else {
            reuseView.backgroundColor = [UIColor brownColor];
        }
        return reuseView;
    }
}
//    } else {
//            UICollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter forIndexPath:indexPath];
//            reuseView.backgroundColor = [UIColor yellowColor];
//            return reuseView;
//        } else if (indexPath.section == 1) {
//            UICollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter forIndexPath:indexPath];
//            reuseView.backgroundColor = [UIColor purpleColor];
//            return reuseView;
//        } else {
//            UICollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooter forIndexPath:indexPath];
//            reuseView.backgroundColor = [UIColor blueColor];
//            return reuseView;
//    }

    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
